/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pro.unal.edu.datasource;

import java.awt.Color;
import org.eclipse.swt.graphics.ImageData;

/**
 *
 * @author Carlos Eduardo Bernal Cárdenas
 * @version 1.0
 * @created 04-oct-2011
 */
public final class JPEGImage extends MyImage {

    public JPEGImage(String pathImage) {
        setPathImage(pathImage);
        loadImageProperties();
    }

    @Override
    protected void loadImageProperties() {
        ImageData imgData = new ImageData(getPathImage());

        byte[] rgbs = imgData.data;
        setWidth(imgData.width);
        setHeight(imgData.height);
        int x = 1;
        int y = imgData.height - 1;
        int grow = 3;
        if (imgData.depth == 32) {
            grow = 4;
        }
        if (imgData.depth == 24) {
            grow = 3;
        }
        int i = 0;
        setDataImageColor(new Color[imgData.width][imgData.height]);
        for (; i <= imgData.bytesPerLine * imgData.height - 1; i += grow) {
            int a = 127;
            int b = (rgbs[i] + 256) % 256;
            int g = (rgbs[i + 1] + 256) % 256;
            int r = (rgbs[i + 2] + 256) % 256;
            if (imgData.depth == 32) {
                a = (rgbs[i + 3] + 128) % 128;
            }
            Color c = new Color(r, g, b);
            getDataImageColor()[(x - 1) % imgData.width][y] = c;
            if ((x % imgData.width) == 0) {
                if (imgData.width % 4 != 0) {
                    i += (4 - (3 * imgData.width % 4));
                }
                y--;
            }
            x++;
        }
//        System.out.println("h:" + getDataImageColor()[0].length + " w:" + getDataImageColor().length);
    }
}
